﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;
using System.Timers;

namespace Reborn_WorldServer.PendingClusterQuerys
{
    public class GlobalWhoRequest
    {
        public Character character;
        private Timer timeoutTimer = new Timer(6000);

        public GlobalWhoRequest(Character character)
        {
            this.character = character;

            timeoutTimer.AutoReset = false;
            timeoutTimer.Elapsed += new ElapsedEventHandler(timeoutTimer_Elapsed);
            timeoutTimer.Start();
        }

        public void send()
        {
            Managers.pendingQueryManager.sendGlobalWho(this);
        }

        private void timeoutTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            Managers.pendingQueryManager.pendingWhoTimeout(this);
        }
        
        private void killTimeout()
        {
            try
            {
                timeoutTimer.Stop();
                timeoutTimer.Dispose();
            }
            catch { }
        }

        public void processResult(List<string> charactersOnline)
        {
            killTimeout();
            if (charactersOnline != null)
            {
                foreach (string characterInfo in charactersOnline)
                    character.SendMessage(characterInfo, "server");
            }
            else
                character.SendMessage("Cluster request timed out!", "server");
        }
    }
}
